#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <sw/redis++/redis++.h>
#include "utill.hpp"

using std::cout;
using std::endl;
using std::set;
using std::string;
using std::vector;
using sw::redis::Redis;

void test1(Redis &redis)
{
    redis.flushall();

    redis.sadd("key", "111");

    redis.sadd("key", {"222", "333", "444"});

    vector<string> values{"555", "666", "777"};
    redis.sadd("key", values.begin(), values.end());

    set<string> result;
    // set里的元素顺序是固定的，指定一个result.end()或者result.begin()或者别的迭代器，效果都一样
    auto it = std::inserter(result, result.end());
    redis.smembers("key", it);

    PrintContainer(result);
}

void test2(Redis &redis)
{
    redis.flushall();

    redis.sadd("key", {"111", "222", "333"});

    bool result = redis.sismember("key", "555");
    cout << "sismember: " << result << endl;

    long long ret = redis.scard("key");
    cout << "scard: " << ret << endl;

    auto ret_spop = redis.spop("key");
    if (ret_spop)
        cout << "spop: " << ret_spop.value() << endl;
    else
        cout << "result无效" << endl;
}

void test3(Redis &redis)
{
    redis.flushall();

    redis.sadd("key1", {"111", "222", "333"});
    redis.sadd("key2", {"111", "222", "444"});

    set<string> result;
    auto it = std::inserter(result, result.end());
    redis.sinter({"key1", "key2"}, it);

    PrintContainer(result);

    long long len = redis.sinterstore("key3", {"key1", "key2"});
    cout << "len: " << len << endl;

    set<string> ret;
    auto iter = std::inserter(ret, ret.begin());
    redis.smembers("key3", iter);

    PrintContainer(ret);
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");

    // test1(redis);
    // test2(redis);
    test3(redis);

    return 0;
}